package lineSearch;

import util.ArrayGenerator;

// make method static
// make constructor private
// generic 泛型
public class LinearSearch_04 {
    private LinearSearch_04() {
    }
    // 只能使用类对象 不是基本 boolean byte char short int long float double

    public static <T> int search(T[] data, T target) {
        for (int i = 0; i < data.length; i++) {
//            if (data[i] == target) return i;
            // data[0...i-1] 中没有找到目标 （循环不变量）
            if (data[i].equals(target)) return i; // 循环体 维持循环不变量
            // "证明"算法正确性
            // 写出正确的代码 __ 如何写出正确的代码
            // 定义清楚函数的功能
        }
        return -1;
    }

    public static void main(String[] args) {
//        int n = 10000000;

        int[] dataSize = {100 * 10000, 1000 * 10000};
        for (int n : dataSize) {

            Integer[] data = ArrayGenerator.generateOrderedArray(n);

            long startTime = System.nanoTime();
            for (int k = 0; k < 100; k++)
                LinearSearch_04.search(data, n);

            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("n " + n + "k" + 100 + time + " s");
        }


//        Integer[] data = {24, 18, 12, 9, 16, 66, 32, 4, 666};
//        int result = lineSearch.LinearSearch_04.search(data, 16);
//        System.out.println(result);
        // CTRL W select current word
        // 666 not in constant pool
//        System.out.println(lineSearch.LinearSearch_04.search(data, 666));

        Student[] students = {new Student("alice"), new Student("bobo"), new Student("chalers")};
        System.out.println(LinearSearch_04.search(students, new Student("bobo")));
    }
}
